<template>
	<view class="page">
		<view class="poster" v-if="!show_poster">
			<lPainter :board="posterObj" ref="painter"></lPainter>
			<view class="footer-btn">
				<view class="" @click="copyShareLink">
					复制链接
				</view>
				<view class="save" @click="toSave">
					保存到相册
				</view>
			</view>
		</view>
		<view class="poster-btn">
			<view :class="{'is-check':check_idx==index}" v-for="(item,index) in footer_arr" :key="index"
				@click="toChoose(index)">
				<image :src="item" mode=""></image>
				<view v-if="check_idx==index"></view>
			</view>
		</view>
		<!-- 生成的图片 -->
		<uni-popup type="center" ref="posterImg" :maskClick="false">
			<view class="poster-img">
				<image :src="path" mode="aspectFill" @click="previewImg"></image>
				<!-- #ifdef H5 -->
				<view class="title">
					长按图片保存到手机
				</view>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<view class="title">
					点击图片保存到手机
				</view>
				<!-- #endif -->
				<view class="close" @click="closePopup">
					<uv-icon name="close"></uv-icon>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import api from '@/api/index2.js';
	import uniPopup from "../../component/uni-popup/uni-popup.vue";
	import lPainter from '../../component/lime-painter/index.vue'
	export default {
		components: {
			lPainter,
			uniPopup
		},
		data() {
			return {
				show_poster: false, //显示海报
				path: '', //生成的图片地址
				posterObj: {
					width: '670rpx',
					height: '1028rpx',
					background: '#fff',
					borderRadius: '16rpx',
					views: []
				}, //画板数据
				footer_arr: [], //底部选项
				check_idx: 0, //底部选中的下标
				userinfo: null,
				shareConfig: null
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				api.getUserInfo().then(res => {
					this.userinfo = res.data;
					this.getShareConfig()
				});
			},
			getShareConfig() {
				api.getShareConfig().then(res => {
					this.footer_arr = res.data.image.map(item => {
						return item.image
					})
					this.shareConfig = res.data
					this.selectImage(0)
				})
			},
			copyShareLink() {
				uni.setClipboardData({
					data: this.shareConfig.url,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: "复制成功"
						})
					}
				});
			},
			selectImage(index) {
				this.posterObj.views = [{
						type: 'image',
						src: this.userinfo.avatar,
						css: {
							left: '32rpx',
							top: '32rpx',
							borderRadius: '50%',
							width: '80rpx',
							height: '80rpx'
						}
					},
					{
						type: 'text',
						text: this.userinfo.nickname,
						css: {
							fontSize: '28rpx',
							fontWeight: 'bold',
							color: '#1A2033',
							lineHeight: '28rpx',
							left: '136rpx',
							top: '39rpx'
						}
					},
					{
						type: 'text',
						text: '在好的时间遇见最美的你',
						css: {
							fontSize: '24rpx',
							color: '#4070FF',
							lineHeight: '24rpx',
							left: '136rpx',
							top: '80rpx'
						}
					},
					{
						type: 'image',
						src: this.footer_arr[index],
						css: {
							left: '35rpx',
							top: '144rpx',
							width: '600rpx',
							height: '600rpx',
							borderRadius: '16rpx'
						}
					},
					{
						type: 'qrcode',
						text: this.shareConfig.qrcode,
						css: {
							left: '245rpx',
							top: '770rpx',
							width: '180rpx',
							height: '180rpx',
						}
					},
					{
						type: 'image',
						src: "../../static/images/logo.png",
						css: {
							left: '305rpx',
							top: '830rpx',
							width: '60rpx',
							height: '60rpx',
						}
					},
					{
						type: 'text',
						text: '————  由小春科技提供技术支持  ————',
						css: {
							left: '0',
							top: '970rpx',
							width: '100%',
							textAlign: 'center',
							fontSize: '24rpx',
							color: '#989FB3',
							lineHeight: '33rpx'
						}
					}
				]
			},
			previewImg() {
				// #ifdef H5
				return;
				// #endif
				uni.previewImage({
					current: this.path,
					urls: [this.path]
				});
			},
			closePopup() {
				this.$refs.posterImg.close();
				this.show_poster = false;
			},
			toSave() {
				uni.showLoading({
					title: '海报生成中',
				})
				const painter = this.$refs.painter;
				painter.canvasToTempFilePath().then(res => {
					this.path = res.tempFilePath;
					this.$refs.posterImg.open();
					this.show_poster = true;
					uni.hideLoading()
				}).catch(error => {
					uni.showToast({
						icon: 'none',
						title: "海报生成失败"
					})
				})
			},
			getImg(r) {

			},
			toChoose(index) {
				this.check_idx = index;
				this.selectImage(index)
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F4F8;
	}

	.page {
		.uni-transition {
			background-color: rgba(0, 0, 0, 0.6)
		}

		.poster-img {
			width: 670rpx;
			height: 1028rpx;
			position: relative;

			.close {
				width: 40px;
				height: 40px;
				top: -50px;
				left: 50%;
				transform: translateX(-50%);
				position: absolute;
				display: flex;
				background-color: #fff;
				border-radius: 50%;
				align-items: center;
				justify-content: center;
			}

			image {
				width: 100%;
				height: 100%;
			}

			.title {
				font-size: 32rpx;
				font-family: PingFang-SC-Bold, PingFang-SC;
				font-weight: bold;
				color: #FFFFFF;
				line-height: 32rpx;
				text-align: center;
				margin-top: 28rpx;
			}
		}

		.poster {
			padding: 24rpx 40rpx;

			.footer-btn {
				margin-top: 24rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				view {
					width: 319rpx;
					height: 66rpx;
					border-radius: 40rpx;
					border: 1px solid #4070FF;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #4070FF;
					line-height: 66rpx;
					text-align: center;
				}

				.save {
					background: #4070FF;
					color: #FFFFFF;
				}
			}
		}

		.poster-btn {
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 140rpx;
			background: #FFFFFF;
			border-radius: 32rpx 32rpx 0px 0px;
			display: flex;
			align-items: center;
			justify-content: space-around;

			>view {
				width: 100rpx;
				height: 100rpx;
				position: relative;
				border-radius: 10rpx;
				border: 4rpx solid #fff;

				&.is-check {
					border: 4rpx solid #4070FF;
				}

				image {
					width: 100%;
					height: 100%;
				}

				view {
					width: 30rpx;
					height: 30rpx;
					position: absolute;
					right: -15rpx;
					bottom: -15rpx;
					background: url('https://s.yun-live.com/images/20210201/311c01265c1aa508418f6bae10d67602.png') no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>